var app = getApp()
Page({
  data: {
    //字数限制
    maxWord:500,
    currentWord:0,
    array: ['请选择','收到产品与所购不符', '产品瑕疵', '质量问题', '少发/漏发','不想要了'],
    // objectArray: [
    //   {
    //     id: 0,
    //     name: '收到产品与所购不符'
    //   },
    //   {
    //     id: 1,
    //     name: '产品瑕疵'
    //   },
    //   {
    //     id: 2,
    //     name: '质量问题'
    //   },
    //   {
    //     id: 3,
    //     name: '少发/漏发'
    //   },
    //   {
    //     id: 4,
    //     name: '不想要了'
    //   }
    // ],
    index: 0,
  },
  onLoad:function(options){
    this.setData({
      name:app.globalData.Order[0].transaction_no,
      time:app.globalData.Order[0].payment_time,
      phone:app.globalData.Order[0].phone
    })
    console.log(options.id)
    this.setData({
      id: options.id
    })
    console.log(app.globalData.Order[0])
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', this.data.array[e.detail.value])
    this.setData({
      index: this.data.array[e.detail.value]
    })
  },
 
  limitWord:function(e){
    var that = this;
    var value = e.detail.value;
    var wordLength = parseInt(value.length); //解析字符串长度转换成整数。
    if (that.data.maxWord < wordLength) {
      return ;
    }
    that.setData({
      currentWord: wordLength 
    });
  },
  formSubmit: function(e) {
    console.info('表单提交携带数据', e.detail.value.content)
    this.setData({
      content: e.detail.value.content
    })
     //只退款
     if(this.data.index != '' && this.data.content != ''){
      if(this.data.id == 1){
        let tableName = 'Order'
        let recordID = app.globalData.Order[0].id // 数据行 id
       console.log(app.globalData.Order[0].id)
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        
       
        product.set('tuihuoleixing', this.data.index.toString())
        product.set('tui', '只退款')
        product.set('tuihuo', 1)
        product.set('content', this.data.content.toString())
        product.update().then(res => {
          // success
          wx.showToast({
            title: '提交成功，服务人员3个工作日内会处理',
            icon: 'none'
          })
          wx.navigateTo({
            url: '/pages/tuikuan/tuikuan',
          })
        }, err => {
          // err
        })
      }
      //退款退货
      else if(this.data.id == 2){
        let tableName = 'Order'
        let recordID = app.globalData.Order[0].id // 数据行 id
        console.log(app.globalData.Order[0].id)
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        
        product.set('tuihuoleixing', this.data.index.toString())
        product.set('tui', '退款退货')
        product.set('tuihuo', 1)
        product.set('content', this.data.content.toString())
        product.update().then(res => {
          // success
          wx.showToast({
            title: '提交成功，服务人员3个工作日内会处理',
            icon: 'none'
          })
          wx.navigateTo({
            url: '/pages/tuikuan/tuikuan',
          })
        }, err => {
          // err
        })
      }
      //换货
      else{
        let tableName = 'Order'
        let recordID = app.globalData.Order[0].id // 数据行 id
        console.log(app.globalData.Order[0].id)
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        
        product.set('tuihuoleixing', this.data.index.toString())
        product.set('tui', '换货')
        product.set('tuihuo', 1)
        product.set('content', this.data.content.toString())
        product.update().then(res => {
          // success
          wx.showToast({
            title: '提交成功，服务人员3个工作日内会处理',
            icon: 'none'
          })
          wx.navigateTo({
            url: '/pages/tuikuan/tuikuan',
          })
        }, err => {
          // err
        })
      }
     }
     else{
      wx.showToast({
        title: '未填写完全，提交失败',
        icon: 'none'
      })
     }
  },
})